package cn.xiongxf.web.controller;

import cn.xiongxf.dto.KillParamDTO;
import cn.xiongxf.service.IKillCourseService;
import cn.xiongxf.domain.KillCourse;
import cn.xiongxf.query.KillCourseQuery;
import cn.xiongxf.result.JsonResult;
import cn.xiongxf.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {

    @Autowired
    public IKillCourseService killCourseService;

    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = killCourseService.selectPage(page);
        return JsonResult.success(new PageList<KillCourse>(page.getTotal(),page.getRecords()));
    }

    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(killCourseService.selectList(null));
    }

    /**
   * 查询单个对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(killCourseService.selectById(id));
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
            killCourseService.deleteById(id);
        return JsonResult.success();
    }

    /**
    * 保存/修改对象
    */
   /* @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody KillCourse killCourse){
        if(killCourse.getId()!=null){
            killCourseService.updateById(killCourse);
        }else{
            killCourseService.insert(killCourse);
        }
        return JsonResult.success();
    }*/

    /**
    * 添加秒杀课程
    */
    @RequestMapping(value="/add",method= RequestMethod.POST)
    public JsonResult add(@RequestBody @Valid KillCourse killCourse){
        killCourseService.add(killCourse);
        return JsonResult.success();
    }

    /**
     * 查询所有发布的秒杀课程
     */
    @RequestMapping(value = "/online/all",method = RequestMethod.GET)
    public JsonResult onlineAll(){
        List<KillCourse> killCourses =  killCourseService.onlineAll();
        return JsonResult.success(killCourses);
    }

    /**
     * 查询単个发布的秒杀课程
     */
    @RequestMapping(value = "/online/one/{killId}/{activityId}",method = RequestMethod.GET)
    public JsonResult onlineOne(
            @PathVariable("killId") Long killId,
            @PathVariable("activityId") Long activityId){
        KillCourse killCourse =  killCourseService.onlineOne(killId);
        return JsonResult.success(killCourse);
    }

    /**
     * 执行秒杀
     */
    @RequestMapping(value = "/kill",method = RequestMethod.POST)
    public JsonResult kill(@RequestBody @Valid KillParamDTO dto){
        String orderNo = killCourseService.kill(dto);
        return JsonResult.success(orderNo);
    }

}
